package youdao;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.json.JSONArray;
import org.json.JSONObject;
import org.weixin4j.message.normal.ImageInputMessage;

/**
 * @author ydy
 * @version 2017/11/15  11:48
 */
public class ORC {

    public String orc(ImageInputMessage msg) throws Exception {
        Map map = new HashMap();
        String url = "http://openapi.youdao.com/ocrapi";
        String appKey = "22dc9a73a2385355";
        String detectType = "10011";
        String imageType = "1";
        String langType = "en";
        String docType = "json";

        String salt = String.valueOf(System.currentTimeMillis());
        String picUrl = null;
//        System.out.println(picUrl);
        String img = getImageStr("http://mmbiz.qpic.cn/mmbiz_jpg/58iaZnUEV7yUNKeQYqw7BwFxEIsAyOBpR3zGvKGa3Z6AIRupxxueQNE22UfkicMQTYEJJZH2tnsmGmSDQibdwI4vA/0");
//        String img = getImageStr(msg.getPicUrl());
        System.out.println("这是一个URL"+picUrl);
        map.put("appKey", appKey);
        map.put("img", img);
        map.put("detectType", detectType);
        map.put("imageType", imageType);
        map.put("langType", langType);
        map.put("salt", salt);
        map.put("docType", docType);
        String sign = md5(appKey + img + salt + "ZgcvSOXgELPY7JnXbVonduHFk2p34gpl");
        map.put("sign", sign);
        String json= requestOCRForHttp(url,map);

        JSONObject jsonObject   = new JSONObject(json);
        JSONObject resultObject = jsonObject.getJSONObject("Result");
        JSONArray regionsArray = resultObject.getJSONArray("regions");
        JSONObject jsonObject1 = regionsArray.getJSONObject(0);
        JSONArray linesArray = jsonObject1.getJSONArray("lines");
        String text = "";
        for (int i=0;i<linesArray.length();i++) {
            JSONObject jsonI = linesArray.getJSONObject(i);
            JSONArray wordsArray = jsonI.getJSONArray("words");
            for (int w=0;w<wordsArray.length();w++) {
                JSONObject jsonW = wordsArray.getJSONObject(w);
                text = text+jsonW.get("text").toString();
            }
            text = text + "\n";
        }
        System.out.println(text);
        return text;
    }

    public static String requestOCRForHttp(String url,Map requestParams) throws Exception {
        String result = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        /**HttpPost*/
        HttpPost httpPost = new HttpPost(url);
        List params = new ArrayList();
        params.add(new BasicNameValuePair("appKey", requestParams.get("appKey").toString()));
        params.add(new BasicNameValuePair("img", requestParams.get("img").toString()));
        params.add(new BasicNameValuePair("detectType", requestParams.get("detectType").toString()));
        params.add(new BasicNameValuePair("imageType", requestParams.get("imageType").toString()));
        params.add(new BasicNameValuePair("langType", requestParams.get("langType").toString()));
        params.add(new BasicNameValuePair("salt", requestParams.get("salt").toString()));
        params.add(new BasicNameValuePair("sign", requestParams.get("sign").toString()));
        params.add(new BasicNameValuePair("docType", requestParams.get("docType").toString()));
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        /**HttpResponse*/
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        try {
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity, "utf-8");
            EntityUtils.consume(httpEntity);
        } finally {
            try {
                if (httpResponse != null) {
                    httpResponse.close();
                }
            } catch (IOException e) {
                e.getMessage();
            }
            return result;
        }
    }
    /**
     * 获得图片的Base64编码
     * @param imgFile
     * @return
     */
    public static String getImageStr(String imgFile)
    {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        return Base64.encode(data);//返回Base64编码过的字节数组字符串
    }
    /**
     * 生成32位MD5摘要
     * @param string
     * @return
     */
    public static String md5(String string) {
        if(string == null){
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F'};
        byte[] btInput = string.getBytes();
        try{
            /** 获得MD5摘要算法的 MessageDigest 对象 */
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            /** 使用指定的字节更新摘要 */
            mdInst.update(btInput);
            /** 获得密文 */
            byte[] md = mdInst.digest();
            /** 把密文转换成十六进制的字符串形式 */
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        }catch(NoSuchAlgorithmException e){
            return null;
        }
    }
}
